Dynamic code injection

ABSTRACT

Embodiments of the present invention disclose an approach for inserting code into a running thread of execution. A computer sets a first set of bits to a first value, wherein the first value indicates that a first set of instructions should be inserted onto a stack. The computer executes a second set of instructions associated with a first safepoint, wherein the second set of instructions comprises one or more instructions to determine if the first set of bits is set to the first value. The computer determines that the first set of bits is set to the first value, and the computer inserts the first set of instructions onto the stack.

STATEMENT REGARDING PRIOR DISCLOSURES BY THE INVENTOR OR A JOINTINVENTOR

Various aspects of the present invention have been disclosed by aninventor or a joint inventor in the product IBM Software DevelopmentKit, Java Technology Edition, Version 8: Beta Program, made publicallyavailable on Apr. 25, 2013. This disclosure is submitted under 35 U.S.C.102(b)(1)(A). The following document is provided in support:

-   -   IBM SDK, Java Technology Edition, V8: Beta Program, latest news        and information.

FIELD OF THE INVENTION

The present invention relates generally to the field of multitenantcomputing, and more particularly to injecting code into running threadsof execution.

BACKGROUND OF THE INVENTION

In multitenant computing environments, multiple client computers runtenant applications in a single instance of a virtual machine, such asthe Java Virtual Machine (JVM). For clients executing applications basedin the Java® programming language, the single virtual machine instanceis the JVM, which executes on a server computer and provides a virtualcomputing environment that accesses the physical resources of the servercomputer while maintaining isolation among the tenant applications andinformation that they access. The JVM executes Java bytecode usingthreads. Threads contain a program counter and a stack. The programcounter keeps track of where the JVM is executing instructions (e.g.,the memory location currently being used) and the number of instructionsthat have been executed on the thread. The stack is a last in first out(LIFO) data structure that contains frames, which include the bytecodedetailing instructions to be performed by the server computer physicalresources.

During execution of the thread, the thread can be at a “safepoint” ornot at a safepoint. When at a safepoint, the thread's representation ofthe JVM is well described, and can be safely manipulated and seen byother threads in the JVM. When the thread reaches a safepoint, thethread may be safely blocked for a period of time in order to performactions that affect all threads, such as garbage collection. Duringcompilation of Java bytecode into compiled code, the compiler candetermine that a safepoint exists at a certain place in the code andinsert additional instructions that instruct the thread executing thecompiled code to perform certain functions outside of the compiledapplication code.

SUMMARY

Embodiments of the present invention disclose a method, computer programproduct, and system for inserting code into a running thread ofexecution. A computer sets a first set of bits to a first value, whereinthe first value indicates that a first set of instructions should beinserted onto a stack. The computer executes a second set ofinstructions associated with a first safepoint, wherein the second setof instructions comprises one or more instructions to determine if thefirst set of bits is set to the first value. The computer determinesthat the first set of bits is set to the first value, and the computerinserts the first set of instructions onto the stack.

BRIEF DESCRIPTION OF THE SEVERAL VIEWS OF THE DRAWINGS

FIG. 1 is a functional block diagram illustrating a multitenantcomputing environment, in accordance with an embodiment of the presentinvention.

FIG. 2 is a flowchart depicting operational steps of a code injectionmodule, on a server computer within the environment of FIG. 1, inaccordance with an embodiment of the present invention.

FIG. 3 is a flowchart depicting operational steps of a masking module,on a server computer within the environment of FIG. 1, in accordancewith an embodiment of the present invention.

FIG. 4 is a flowchart depicting operational steps of a resourceallocation module, on a server computer within the environment of FIG.1, in accordance with an embodiment of the present invention.

FIG. 5 depicts a block diagram of components of the server computerexecuting the code injection module and the masking module, inaccordance with an embodiment of the present invention.

DETAILED DESCRIPTION

Embodiments of the present invention recognize that, in virtualized,multitenant environments, certain operations may need to be performedthat affect all threads and not just the thread(s) of a single tenantapplication. For example, in the Java Virtual Machine (JVM), resourcemanagement may include allocating access to memory or CPU resourcesacross tenant applications in order to ensure that one tenant does notdominate CPU capacity while other tenant applications lack necessary CPUcapabilities. Embodiments of the present invention disclose injectingcode into a running thread to achieve a desired behavior. Variousembodiments may be useful for, among other applications, computingresource allocation, statistics gathering, determining stack depth,and/or controlling the lifetime of particular threads.

The present invention will now be described in detail with reference tothe Figures. FIG. 1 is a functional block diagram illustrating amultitenant computing environment (“environment”), generally designated100, in accordance with an embodiment of the present invention.

Environment 100 includes server computer 130 and client computers 120and 124, all interconnected over network 110. Network 110 can be, forexample, a local area network (LAN), a wide area network (WAN) such asthe Internet, or a combination of the two, and can include wired,wireless, or fiber optic connections. In general, network 110 can be anycombination of connections and protocols that will supportcommunications between server computer 130 and client computers 120 and124.

In various embodiments of the present invention, client computers 120and 124, as well as server computer 130, can be servers, laptopcomputers, tablet computers, netbook computers, personal computers(PCs), desktop computers, personal digital assistants (PDAs) smartphones, or any programmable electronic devices capable of communicatingover network 110. In another embodiment, server computer 130 and clientcomputers 120 and 124 represent a computing system utilizing clusteredcomputers and components to act as a single pool of seamless resourceswhen accessed by elements of environment 100. In general, servercomputer 130 can be any computing device or combination of devices withaccess to thread 134, code injection module 148, masking module 150,resource allocation module 152, and CPU allocation accumulator 154, andcapable of running code injection module 148, masking module 150, andresource allocation module 152. Server computer 130 may include internaland external hardware components, as depicted and described in furtherdetail with respect to FIG. 5.

Server computer 130 includes server operating system 160, hardwareresources 170, and JVM 132. In some embodiments, server operating system160 is the native operating system of server computer 160. In variousembodiments, server operating system 160 includes an applicationprogramming interface (API) which allows JVM 132 to access and allocatehardware resources 170, such as CPU 172 and physical memory 174, for useby tenant applications 122 and 126. In various embodiments, CPU 172 andphysical memory 174 are shared physical resources that tenantapplications 122 and 126 can access by interacting with JVM 132. In someembodiments, server operating system 160 can run programs, such asapplication services or daemons, which apply to all tenant applicationsrunning in JVM 132.

JVM 132 is a managed runtime environment that executes computerinstructions written in Java bytecode by interpreting and/or compilingthe instructions into machine code that hardware resources 170 caninterpret. JVM 132 performs just-in-time (JIT) compilation on aJava-based programing language. This process converts the Java-basedsource code into object code that CPU 172 can interpret to performvarious computer functions, such as arithmetic, read, and writefunctions. Additionally, JVM 132 manages one or more threads ofexecution associated with tenant applications 122 and 126, such asthreads 134 and 156.

JVM 132 includes tenant applications 122 and 126. Tenant application 122is associated with a single client computer, such as client computer120, and tenant application 126 is associated with a different clientcomputer, such as client computer 124. In various embodiments, tenantapplications 122 and 126 represent computer readable softwareapplications that access a shared pool of hardware resources in servercomputer 130 through a single, shared instance of JVM 132 operating onserver computer 130. In various embodiments, tenant applications 122 and126 are computer software code written in a programming language forcompilation by JVM 132, such as, Java®, Jython®, Kotlin®, JRuby®, orother languages.

Tenant application 122 includes thread 134. Thread 134 is a mechanismwithin JVM 132 for executing a discrete sequence of programmedinstructions. In various embodiments, thread 134 can include a programcounter and/or a stack data structure, such as stack 136. In someembodiments, each tenant application, such as tenant applications 122and 126, includes one or more associated threads. Additionally, in someembodiments, additional threads may be maintained by JVM 132 and/orserver operating system 160 for operations such as garbage collection orhardware resource management. In various embodiments, other tenantapplications, such as tenant application 126, include one or moreassociated threads, such as thread 156. Thread 156 further includesanalogous contents, such as one or more stack data structures and aninject bit (not shown).

Thread 134 includes stack 136 and inject bit 144. Stack 136 comprisesone or more frames, such as frame 138. Stack 136 is a “last in firstout” (LIFO) data structure. In various embodiments, a frame can be addedto the stack (pushed) for execution. Similarly, when a frame completesexecution, the frame is removed (popped) from the top of the stack andthe thread proceeds to process the frame below the popped frame. Invarious embodiments, frames include the necessary class information andreference information to modify and alter local variables. In variousembodiments, stack 136 can include an associated bit that, whenactivated, masks the thread execution. Mask frame 140 includes programinstructions to activate the mask bit to indicate that interruption ofthe thread execution may result in an exception. Safepoint 142 is acommon component in the JVM 132 that indicates a particular point inthread execution at which the execution may be suspended for a period oftime without disrupting the execution of the thread. For example,safepoints are commonly used to halt thread execution for the purposesof garbage collection. Safepoints occur at various intervals in thecode, such as, after a return from method call or at a back jump of aloop. In various embodiments, safepoints can be identified at compiletime by a compiler (e.g., a just-in-time compiler), and the compiler caninsert program instructions for the thread executing the instructions toperform functions outside of the original application code. In someembodiments of the present invention, the instructions added at thesafepoint include instructions to check one or more bits of information(e.g., inject bit 144) that, when set to a particular value, indicatethat a set of program instructions should be injected onto the stack ofthe executing thread. In various embodiments of the present invention,JVM 132 injects a runnable interface at a thread safepoint in order toinstruct the thread to execute the runnable method.

Inject bit 144 is one or more bits of information stored in associationwith thread 134 and, when set to a particular value, instructs thread134 to push a runnable piece of code, such as a method onto the top ofstack 136 when a safepoint is reached. In an embodiment, thread 134executes the program instructions in stack 136 until a safepoint isreached. In this embodiment, the safepoint includes instructions tocheck a series of bits stored in association with thread 134. In thisembodiment, one such bit (e.g., inject bit 144) instructs thread 134 topush a runnable method onto the stack for execution by the thread beforereturning to the code that was running when the thread reached thesafepoint. In various embodiments, inject bit 144 can be set forsafepoints occurring at regular time intervals. For example, inject bit144 may be set to trigger injection once per second, but any safepointsthat occur more frequently than once per second would have inject bit144 set to a value that does not trigger injection. In this example, thethread will execute the injected runnable method at a regular interval.The injected runnable may include instructions to collect statistics,manage resource allocation, or terminate a thread.

JVM 132 includes heap memory 146, code injection module 148, maskingmodule 150, resource allocation module 152, and CPU allocationaccumulator 154.

Heap memory 146 is a memory data structure that is used to allocateclass instances and arrays at runtime. In various embodiments, heapmemory 146 provides for dynamic memory allocation that is divided intogenerations. In some embodiments, heap memory 146 has a young generationfor short-lived objects that are created and then immediately garbagecollected, and an old generation for objects that are used repeatedly byone or more threads.

Code injection module 148 is software capable of pushing a frame ofrunnable code onto stack 136 of thread 134. Additionally, code injectionmodule 148 is capable of communicating with threads associated withother tenants operating on JVM 132. In various embodiments, codeinjection module 148 determines that thread 136 has reached a safepoint,checks inject bit 144 to determine if an injection of code into stack136 should occur, and if so, injects a sequence of runnable code intothe stack of the thread, which then executes the injected code beforepopping the runnable code and returning to code that was executing priorto the safepoint.

Masking module 150 is software capable of marking a particular thread asunsafe to halt for a code injection. In various embodiments, maskingmodule 150 determines that a mask was called, that stack 136 reaches asafepoint, and that inject bit 144 is set, then masking module 150clears the inject bit and sets a pending inject bit, which thread 134will check when an unmask method is called.

Resource allocation module 152 is an example implementation of codeinjection module 148 directed to a specific embodiment in the context ofresource allocation of CPU 172 time amongst tenant applications 122 and126.

CPU allocation accumulator 154 is a data structure that maintains theallotment granted to each tenant computer by JVM 132. In variousembodiments, CPU allocation accumulator 154 includes the number ofcycles of CPU 172 that tenant application 122 is, on average, permittedto use in a defined period of time. In certain embodiments, CPUallocation accumulator 154 maintains a running total of the number ofcycles used in the specified time. Resource allocation module 152 checksthe status of running totals at safepoints, such as safepoint 142, andif tenant application 122 exceeds its allotment, then resourceallocation module 152 ceases execution of thread 134 until tenantapplication 122 receives an additional allotment of CPU cycles at theend of the defined period of time.

FIG. 2 is a flowchart depicting operational steps of code injectionmodule 148 operating within JVM 132, in accordance with an embodiment ofthe present invention. Code injection module 148 provides a computerimplemented mechanism for safely halting executing threads in amultitenant JVM in order to inject runnable code onto the stack of oneor more running threads. In various embodiments, code injection module148 instructs JVM 132 to perform actions and manipulate data which maybe carried out, for example, by thread 134, or an independent threadoperating within JVM 132.

JVM 132 determines that thread 134 reaches a safepoint in its execution,such as safepoint 142 (step 202). Safepoints in threads executing withinthe JVM occur at predetermined times, for example, at the invocation ofa particular method. Safepoints indicate particular points in theexecution of the thread at which operations other than the code of theexecuting tenant application, such as tenant applications 122 and 126,may be safely executed by the thread. In one embodiment of the presentinvention, safepoints include instructions to perform maintenancefunctions or other statistics gathering functions. In an embodiment, agiven safepoint includes instructions to check one or more bits ofinformation stored within the thread to determine if any of themaintenance functions or statistics gathering functions should beperformed while the thread is at a safepoint.

JVM 132 determines whether the inject bit is set (decision block 204).In one embodiment, one or more of the bits, such as inject bit 144,checked during the safepoint includes instructions to inject, or push, aparticular piece of runnable code to the top of stack 136. In variousembodiments, the injected code can include statistics gathering code orresource allocation code, such as resource allocation module 152. Invarious embodiments, inject bit 144 can be set to instruct JVM 132 toinject the runnable code at a safepoint or set to instruct JVM 132 notto inject any runnable code at this safepoint. In some embodiments,inject bit 144 can be set to inject runnable code at predetermined timeintervals, such as once per second. If JVM 132 determines that theinjection bit is not set to inject runnable code into stack 136(decision block 204, NO branch), then JVM 132 waits until the nextsafepoint is reached in step 202. If JVM 132 determines that inject bit144 bit is set and a runnable code should be injected into the stack atthe safepoint, then JVM 132 proceeds to push a segment of runnable codeto the top of the stack in step 206.

JVM 132 pushes a segment of runnable code to the top of stack 136 (step206). In various embodiments of the present invention, the segment ofrunnable code can be an extension of the interface java.lang.Runnable(). In other embodiments, the runnable code can be injected using anytype of code, method, or function that will initiate a runnable sequenceof code. The runnable interface provides a mechanism for inserting arunnable method onto stack 136 as well as other stacks that may beassociated with other tenant applications. In various embodiments,extensions of the runnable interface include runnable methods thatinstruct the thread to perform various functions such as collectstatistics, perform resource allocation functions, terminate a thread,or other functions.

JVM 132 executes the injected, runnable code (step 208). Thread 134,operating within tenant application 122 in JVM 132, now having arunnable method frame as at the top of stack 134, executes theinstructions pushed to the top of stack 134. In various embodiments, therunnable code includes Java bytecode instructions that may relate tosuch functions as statistic collection, resource management, and/orthread termination. In various embodiments, the injected code is limitedto code that will not break the tenant application code that was runningprior to the injection. For example, allowing input/output resources inthe injected code can cause exceptions if the code executing prior tothe code injection was already using the input/output resources. Inother embodiments, ensuring that the code being injected ispre-resolved, and that all class and/or field references have beeninitialized, avoids running a ClassLoader infrastructure, which may leadto various exceptions or errors.

JVM 132 pops the runnable method frame from stack 136 and returns toexecution of the code in stack 136 on thread 134 that was running priorto the code injection (step 210). At the end of the runnable methodexecuted in step 208, the code instructions include instructions to pop(i.e., remove) the frame containing the injected code from stack 136. Insome embodiments, following the removal of the injected code, thread 134returns to executing the frames that populated stack 136 prior to theinjection of the runnable code. In other embodiments, the runnable codeincludes instructions to terminate thread 134, in which case, followingthe execution of the runnable code, stack 136 pops the frame containingthe runnable method and thread 134 terminates.

FIG. 3 is a flowchart depicting operational steps of masking module 150operating within JVM 132, in accordance with an embodiment of thepresent invention. Embodiments of masking module 150 recognize thatthere are periods in thread execution during which halting the threadand injecting code into the thread could result in errors and/orexceptions occurring in both the injected code and when the threadattempts to return to normal execution following the removal of theinjected code. In various embodiments, masking module 150 instructs JVM132 to perform actions and manipulate data which may be carried out, forexample, by thread 134, or an independent thread operating within JVM132.

JVM 132 calls a mask for thread 134 (step 302). In one embodiment,thread 134 includes a plurality of associated bits of information. Insome embodiments, one of the associated bits of information indicateswhether or not a mask was called during the execution of the pluralityof frames in stack 136, for example, by executing the code included inmask 140. In one embodiment, a line of computer code executing on thread134, such as a method of Java bytecode, instructs CPU 172 to set theassociated bit that indicates whether or not a thread is masked (“maskbit”) to a predefined value, wherein the value indicates that the threadis masked. In various embodiments, masking marks the thread so that codeinjection module 148 does not instruct JVM 132 to inject any code ontostack 136 even if thread 134 is at a safepoint and inject bit 144 is setto indicate that an injection should occur.

JVM 132 determines that thread 134 reached a safepoint in execution(step 304). Safepoints in threads executing within the JVM occur atpredetermined times, for example, at the invocation of a particularmethod. Safepoint 142 indicates particular points in the execution ofthread 134 at which operations other than the code of tenant application122 may be safely executed by thread 134. In one embodiment of thepresent invention, safepoint 142 includes instructions to performmaintenance functions or other statistics gathering functions. Inanother embodiment, safepoint 142 includes instructions to check one ormore bits information stored within the thread to determine if any ofthe maintenance functions or statistics gathering functions should beperformed while the thread is at safepoint 142.

JVM 132 determines whether inject bit 144 is set (decision block 306).In one embodiment, one or more of the bits checked during safepoint 142includes instructions to inject, or push, a particular piece of runnablecode to the top stack 136. In various embodiments, this bit can be setto instruct JVM 132 to inject the runnable code at a safepoint or be setto instruct JVM 132 not to inject any runnable code at this safepoint.In some embodiments, the inject bit can be set to inject runnable codeat predetermined time intervals, such as once per second. If JVM 132determines that the inject bit is not set (decision block 306, NObranch), then JVM 132 continues to monitor thread activity until anothersafepoint occurs in step 304. If JVM 132 determines that the inject bitis set and a sequence of runnable code should be injected onto stack 136(decision block 306, YES branch), then JVM 132 clears the inject bit andsets a separate, pending inject bit in step 308.

JVM 132 clears the inject bit and sets the pending inject bit (step308). In some embodiments, JVM 132 does not inject the runnable codeonto stack 136 because the mask bit indicates that an injection couldresult in errors and/or exceptions. In various embodiments, JVM 132clears inject bit 144 and sets a pending inject bit, which is distinctand separate from inject bit 144, to active so that when thread 134executes an unmask method call, thread 134 checks the pending injectbit, which indicates that there is a runnable code that should beinjected onto stack 136. In alternative embodiments, JVM 132 does notinject the runnable code, but resets inject bit 144 to an activeposition, so that at the next safepoint, thread 134 checks inject bit144 again and determines whether the thread is masked by checking themask bit. In various embodiments, the process of resetting inject bit144 to active repeats for subsequent safepoints until an unmask methodis called for thread 134. Once the unmask method is called, the nextsafepoint will result in the injection of the runnable section of codeonto stack 136.

JVM 132 determines that an unmask method has been called (step 310). Anunmask is called when executable bytecode instructs JVM 132 todeactivate the associated mask bit and determine if the pending injectbit is set. In response to determining that the pending inject bit isset, JVM 132 proceeds to execute code injection module 148, as describedin FIG. 2, or a functional equivalent thereof (step 312).

In other embodiments, JVM 132 can attempt to set the inject bit toactive after a mask has already been called on thread 134. In theseembodiments, JVM 132 can check the mask bit stored in association withthread 134 to determine that thread 134 is masked. In response todetermining that the thread 134 is masked, JVM 132 can set the pendinginject bit to active as described in step 308 of FIG. 3. In theseembodiments of the present invention, an unmask method is called asdescribed in step 310 of FIG. 3. Responsive to determining that anunmask method was called, these embodiments proceed to execute codeinjection module 148, as in step 312 of FIG. 3.

FIG. 4 is a flowchart depicting operational steps of resource allocationmodule 152, in accordance with an embodiment of the present invention.Resource allocation module 152 is an example embodiment of an acceptablesequence of code instructions that may be injected onto stack 136 inaccordance with code injection module 148 and/or masking module 150. Tobegin with, JVM 132 determines that a safepoint was reached by thread134, inject bit 144 was set, and code injection module 148 instructedJVM 132 to push resource allocation module 152 to the top of stack 136.Thread 134 begins execution of the program instructions that make upresource allocation module 152.

Thread 134 transmits a request for the current CPU allocation withrespect to tenant application 122 (step 402). In one embodiment, thread134 transmits a request to CPU allocation accumulator 154 to return thecurrent CPU allocation for the tenant application associated with thethread on which resource allocation module 152 is executing. In variousembodiments, CPU allocation accumulator 154 maintains a running total ofthe CPU allocation for each tenant application in the memory of JVM 132.In these embodiments, CPU allocation accumulator 154 can also read andtransmit the current state of the CPU allocation for each thread thattransmits a request. CPU allocation may be measured in, for example,cycles per second.

Thread 134 determines whether a tenant exceeded its CPU allocation forthe current allocation period (decision block 404). Thread 134 analyzesthe received CPU allocation to determine whether the running total ofcycles that tenant application 122 actually used in the currentallocation period exceeded the maximum amount of cycles allocated totenant application 122 for the time period. In some embodiments, CPUallocation accumulator 154 begins a time period with the maximum numberof cycles allowed by each tenant. As the tenant uses cycles, CPUallocation accumulator 154 subtracts those cycles from the initialamount to maintain a running total. If tenant application 122 exceedsits allotment for the time period, then the running total will reachzero cycles (or negative cycles) during the time period. In otherembodiments, CPU allocation accumulator 154 may begin each time periodwith zero cycles for each tenant application. In the other embodiments,thread 134 compares the number of cycles for the tenant at the time ofthe request with a maximum number of cycles allowed for the tenant,which may be, for example, a constant number stored in association withthread 134 or in heap memory 146. If thread 134 determines that a tenantdid not exceed its CPU allocation for the current time period (decisionblock 404, NO branch), then thread 134 returns to request the currentCPU allocation for a subsequent time period in step 402. If thread 134determines that tenant application 122 did exceed its CPU allocation forthe current time period (decision block 404, YES branch), then thread134 suspends execution of tenant application 122 for the duration of thenext allocation time period in step 406.

Thread 134 suspends execution of tenant application (step 406). In oneembodiment, thread 134 creates an await condition, which stops thethread execution until the thread receives a signal that an additionalallocation of CPU resources has been granted to tenant application 122.

Thread 134 receives a signal that tenant application 122 received anadditional CPU allocation (step 408). In an embodiment, CPU allocationaccumulator 154 resets so that tenant application 122 receives anadditional allocation of CPU cycles at a regular interval of time, forexample, once per second. In this embodiment, the program code thatresets the CPU allocation for the tenant application can include aprogram instruction to transmit a signal to thread 134 that anadditional allocation has occurred, and thread 134 can terminate theawait condition and resume execution of the thread. In response tothread 134 receiving the signal that the await condition has beensatisfied, then thread 134 resumes thread execution for the threadsassociated with tenant application 122 in step 410.

FIG. 5 depicts a block diagram of components of server computer 130 inaccordance with an illustrative embodiment of the present invention. Itshould be appreciated that FIG. 5 provides only an illustration of oneimplementation and does not imply any limitations with regard to theenvironments in which different embodiments may be implemented. Manymodifications to the depicted environment may be made.

Server computer 130 includes communications fabric 502, which providescommunications between computer processor(s) 504, memory 506, persistentstorage 508, communications unit 510, and input/output (I/O)interface(s) 512. Communications fabric 502 can be implemented with anyarchitecture designed for passing data and/or control informationbetween processors (such as microprocessors, communications and networkprocessors, etc.), system memory, peripheral devices, and any otherhardware components within a system. For example, communications fabric502 can be implemented with one or more buses.

Memory 506 and persistent storage 508 are computer-readable storagemedia. In this embodiment, memory 506 includes random access memory(RAM) 514 and cache memory 516. In general, memory 506 can include anysuitable volatile or non-volatile computer-readable storage media.

Code injection module 148 and masking module 150 are stored inpersistent storage 508 for execution by one or more of the respectivecomputer processors 504 via one or more memories of memory 506. In thisembodiment, persistent storage 508 includes a magnetic hard disk drive.Alternatively, or in addition to a magnetic hard disk drive, persistentstorage 508 can include a solid state hard drive, a semiconductorstorage device, read-only memory (ROM), erasable programmable read-onlymemory (EPROM), flash memory, or any other computer-readable storagemedia that is capable of storing program instructions or digitalinformation.

The media used by persistent storage 508 may also be removable. Forexample, a removable hard drive may be used for persistent storage 508.Other examples include optical and magnetic disks, thumb drives, andsmart cards that are inserted into a drive for transfer onto anothercomputer-readable storage medium that is also part of persistent storage508.

Communications unit 510, in these examples, provides for communicationswith other data processing systems or devices. In these examples,communications unit 510 includes one or more network interface cards.Communications unit 510 may provide communications through the use ofeither or both physical and wireless communications links. Codeinjection module 148 and masking module 150 may be downloaded topersistent storage 508 through communications unit 510.

I/O interface(s) 512 allows for input and output of data with otherdevices that may be connected to server computer 130. For example, I/Ointerface 512 may provide a connection to external devices 518 such as akeyboard, keypad, a touch screen, and/or some other suitable inputdevice. External devices 518 can also include portable computer-readablestorage media such as, for example, thumb drives, portable optical ormagnetic disks, and memory cards. Software and data used to practiceembodiments of the present invention, e.g., code injection module 148and masking module 150, can be stored on such portable computer-readablestorage media and can be loaded onto persistent storage 508 via I/Ointerface(s) 512. I/O interface(s) 512 also connect to a display 520.

Display 520 provides a mechanism to display data to a user and may be,for example, a computer monitor.

The programs described herein are identified based upon the applicationfor which they are implemented in a specific embodiment of theinvention. However, it should be appreciated that any particular programnomenclature herein is used merely for convenience, and thus theinvention should not be limited to use solely in any specificapplication identified and/or implied by such nomenclature.

The flowchart and block diagrams in the Figures illustrate thearchitecture, functionality, and operation of possible implementationsof systems, methods, and computer program products according to variousembodiments of the present invention. In this regard, each block in theflowchart or block diagrams may represent a module, segment, or portionof code, which comprises one or more executable instructions forimplementing the specified logical function(s). It should also be notedthat, in some alternative implementations, the functions noted in theblock may occur out of the order noted in the figures. For example, twoblocks shown in succession may, in fact, be executed substantiallyconcurrently, or the blocks may sometimes be executed in the reverseorder, depending upon the functionality involved. It will also be notedthat each block of the block diagrams and/or flowchart illustration, andcombinations of blocks in the block diagrams and/or flowchartillustration, can be implemented by special purpose hardware-basedsystems that perform the specified functions or acts, or combinations ofspecial purpose hardware and computer instructions.

The present invention may be a system, a method, and/or a computerprogram product. The computer program product may include a computerreadable storage medium (or media) having computer readable programinstructions thereon for causing a processor to carry out aspects of thepresent invention.

The computer readable storage medium can be a tangible device that canretain and store instructions for use by an instruction executiondevice. The computer readable storage medium may be, for example, but isnot limited to, an electronic storage device, a magnetic storage device,an optical storage device, an electromagnetic storage device, asemiconductor storage device, or any suitable combination of theforegoing. A non-exhaustive list of more specific examples of thecomputer readable storage medium includes the following: a portablecomputer diskette, a hard disk, a random access memory (RAM), aread-only memory (ROM), an erasable programmable read-only memory (EPROMor Flash memory), a static random access memory (SRAM), a portablecompact disc read-only memory (CD-ROM), a digital versatile disk (DVD),a memory stick, a floppy disk, a mechanically encoded device such aspunch-cards or raised structures in a groove having instructionsrecorded thereon, and any suitable combination of the foregoing. Acomputer readable storage medium, as used herein, is not to be construedas being transitory signals per se, such as radio waves or other freelypropagating electromagnetic waves, electromagnetic waves propagatingthrough a waveguide or other transmission media (e.g., light pulsespassing through a fiber-optic cable), or electrical signals transmittedthrough a wire.

Computer readable program instructions described herein can bedownloaded to respective computing/processing devices from a computerreadable storage medium or to an external computer or external storagedevice via a network, for example, the Internet, a local area network(LAN), a wide area network (WAN), and/or a wireless network. The networkmay comprise copper transmission cables, optical transmission fibers,wireless transmission, routers, firewalls, switches, gateway computersand/or edge servers. A network adapter card or network interface in eachcomputing/processing device receives computer readable programinstructions from the network and forwards the computer readable programinstructions for storage in a computer readable storage medium withinthe respective computing/processing device.

Computer readable program instructions for carrying out operations ofthe present invention may be assembler instructions,instruction-set-architecture (ISA) instructions, machine instructions,machine dependent instructions, microcode, firmware instructions,state-setting data, or either source code or object code written in anycombination of one or more programming languages, including an objectoriented programming language such as, Smalltalk, C++ or the like, andconventional procedural programming languages, such as the “C”programming language or similar programming languages. The computerreadable program instructions may execute entirely on the user'scomputer, partly on the user's computer, as a stand-alone softwarepackage, partly on the user's computer and partly on a remote computer,or entirely on the remote computer or server. In the latter scenario,the remote computer may be connected to the user's computer through anytype of network, including a local area network (LAN) or a wide areanetwork (WAN), or the connection may be made to an external computer(for example, through the Internet using an Internet Service Provider).In some embodiments, electronic circuitry including, for example,programmable logic circuitry, field-programmable gate arrays (FPGA), orprogrammable logic arrays (PLA) may execute the computer readableprogram instructions by utilizing state information of the computerreadable program instructions to personalize the electronic circuitry,in order to perform aspects of the present invention.

Aspects of the present invention are described herein with reference toflowchart illustrations and/or block diagrams of methods, apparatus(systems), and computer program products according to embodiments of theinvention. It will be understood that each block of the flowchartillustrations and/or block diagrams, and combinations of blocks in theflowchart illustrations and/or block diagrams, can be implemented bycomputer readable program instructions.

These computer readable program instructions may be provided to aprocessor of a general purpose computer, special purpose computer, orother programmable data processing apparatus to produce a machine, suchthat the instructions, which execute via the processor of the computeror other programmable data processing apparatus, create means forimplementing the functions/acts specified in the flowchart and/or blockdiagram block or blocks. These computer readable program instructionsmay also be stored in a computer readable storage medium that can directa computer, a programmable data processing apparatus, and/or otherdevices to function in a particular manner, such that the computerreadable storage medium having instructions stored therein comprises anarticle of manufacture including instructions which implement aspects ofthe function/act specified in the flowchart and/or block diagram blockor blocks.

The computer readable program instructions may also be loaded onto acomputer, other programmable data processing apparatus, or other deviceto cause a series of operational steps to be performed on the computer,other programmable apparatus, or other device to produce a computerimplemented process, such that the instructions which execute on thecomputer, other programmable apparatus, or other device implement thefunctions/acts specified in the flowchart and/or block diagram block orblocks.

The flowchart and block diagrams in the Figures illustrate thearchitecture, functionality, and operation of possible implementationsof systems, methods, and computer program products according to variousembodiments of the present invention. In this regard, each block in theflowchart or block diagrams may represent a module, segment, or portionof instructions, which comprises one or more executable instructions forimplementing the specified logical function(s). In some alternativeimplementations, the functions noted in the block may occur out of theorder noted in the figures. For example, two blocks shown in successionmay, in fact, be executed substantially concurrently, or the blocks maysometimes be executed in the reverse order, depending upon thefunctionality involved. It will also be noted that each block of theblock diagrams and/or flowchart illustration, and combinations of blocksin the block diagrams and/or flowchart illustration, can be implementedby special purpose hardware-based systems that perform the specifiedfunctions or acts or carry out combinations of special purpose hardwareand computer instructions.

What is claimed is:
 1. A computer program product for inserting codeinto a running thread of execution, the computer program productcomprising: one or more computer-readable storage media and programinstructions stored on the one or more computer-readable storage media,the program instructions comprising: program instructions to set a firstone or more bits to a first value, wherein the first value indicatesthat no set of instructions should be inserted onto a stack of a threaduntil the first one or more bits is set to a second value; programinstructions to execute a first set of instructions associated with afirst safepoint, wherein the first set of instructions comprises one ormore instructions to determine if a second one or more bits is set to athird value, wherein the third value indicates that a second set ofinstructions should be inserted onto the stack of the thread; programinstructions to determine that the second one or more bits is set to thethird value; program instructions to set a third one or more bits to afourth value, wherein the fourth value indicates that the second set ofinstructions should be inserted onto the stack, based at least in parton execution of a third set of instructions associated with a secondsafepoint; program instructions to set the first one or more bits to thesecond value, wherein the second value indicates that the thread cansafely execute the third set of instructions associated with the secondsafepoint and insert the second set of instructions onto the stack;program instructions to execute the third set of instructions associatedwith the second safepoint; and program instructions to insert the secondset of instructions onto the stack.
 2. The computer program product ofclaim 1, further comprising: program instructions to execute the secondset of instructions; and program instructions to remove the second setof instructions from the stack.
 3. The computer program product of claim2, wherein the second set of instructions comprises: programinstructions to determine that the thread exceeded a resource allocationlimit; and program instructions to suspend execution of the thread for aperiod of time.
 4. The computer program product of claim 3, wherein thesecond set of instructions further comprises: program instructions toreceive a signal indicating that the period of time has ended; andprogram instructions to resume execution of the thread.
 5. The computerprogram product of claim 3, wherein determining that the thread exceededthe resource allocation limit comprises comparing the resourceallocation limit with a current number of cycles that the thread hasexecuted.
 6. The computer program product of claim 1, wherein the secondset of instructions comprises a well-defined set of computer programinstructions predetermined to be safe to insert onto the stack.
 7. Thecomputer program product of claim 1, wherein the thread is anapplication thread of a tenant application executing in a multitenantvirtual machine.
 8. The computer program product of claim 1, wherein thesecond set of instructions comprise a runnable method to perform one ormore functions of: statistic collection, resource management, or threadtermination.
 9. A computer system for inserting code into a runningthread of execution, the computer system comprising: one or morecomputer processors; one or more computer-readable storage media;program instructions stored on the computer-readable storage media forexecution by at least one of the one or more processors, the programinstructions comprising: program instructions to set a first one or morebits to a first value, wherein the first value indicates that no set ofinstructions should be inserted onto a stack of a thread until the firstone or more bits is set to a second value; program instructions toexecute a first set of instructions associated with a first safepoint,wherein the first set of instructions comprises one or more instructionsto determine if a second one or more bits is set to a third value,wherein the third value indicates that a second set of instructionsshould be inserted onto the stack of the thread; program instructions todetermine that the second one or more bits is set to the third value;program instructions to set a third one or more bits to a fourth value,wherein the fourth value indicates that the second set of instructionsshould be inserted onto the stack, based at least in part on executionof a third set of instructions associated with a second safepoint;program instructions to set the first one or more bits to the secondvalue, wherein the second value indicates that the thread can safelyexecute the third set of instructions associated with the secondsafepoint and insert the second set of instructions onto the stack;program instructions to execute the third set of instructions associatedwith the second safepoint; and program instructions to insert the secondset of instructions onto the stack.
 10. The computer system of claim 9,wherein the program instructions stored on the one or more computerreadable storage media further comprise: program instructions to executethe second set of instructions; and program instructions to remove thesecond set of instructions from the stack.
 11. The computer system ofclaim 10, wherein the second set of instructions comprises: programinstructions to determine that the thread exceeded a resource allocationlimit; and program instructions to suspend execution of the thread for aperiod of time.
 12. The computer system of claim 11, wherein the secondset of instructions further comprises: program instructions to receive asignal indicating that the period of time has ended; and programinstructions to resume execution of the thread.
 13. The computer systemof claim 11, wherein determining that the thread exceeded the resourceallocation limit comprises comparing the resource allocation limit witha current number of cycles that the thread has executed.
 14. Thecomputer system of claim 9, wherein the second set of instructionscomprises a well-defined set of computer program instructionspredetermined to be safe to insert onto the stack.
 15. The computersystem of claim 9, wherein the thread is an application thread of atenant application executing in a multitenant virtual machine.
 16. Thecomputer system of claim 9, wherein the second set of instructionscomprise a runnable method to perform one or more functions of:statistic collection, resource management, or thread termination.